package cn.est.serviceImpl;

import cn.est.constants.Constants;
import cn.est.constants.MathUtil;
import cn.est.mapper.SmsLogMapper;
import cn.est.pojo.SmsLog;
import cn.est.service.SmsLogService;
import cn.est.dto.Page;
import cn.est.utils.RedisUtil;
import cn.est.utils.SMSutil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

@Service
@Transactional //支持事务回滚
public class SmsLogServiceImpl implements SmsLogService {

    @Autowired
    private SmsLogMapper smsLogMapper;

    @Autowired
    private SMSutil smSutil;

    @Autowired
    private RedisUtil redisUtils;

    public SmsLog getSmsLogById(Long id) {
        return smsLogMapper.getSmsLogById(id);
    }

    public List<SmsLog> getSmsLogListByMap(Map<String, Object> param) {
        return smsLogMapper.getSmsLogListByMap(param);
    }

    public Integer getSmsLogCountByMap(Map<String, Object> param) {
        return smsLogMapper.getSmsLogCountByMap(param);
    }

    public Integer insertSmsLog(SmsLog smsLog) {
        //smsLog.setCreatdTime(new Date());
        return smsLogMapper.insertSmsLog(smsLog);
    }

    public Integer updateSmsLog(SmsLog smsLog) {
        //smsLog.setUpdatedTime(new Date());
        return smsLogMapper.updateSmsLog(smsLog);
    }

    public Integer deleteSmsLogById(Long id) {
        return smsLogMapper.deleteSmsLogById(id);
    }

    public Integer batchDeleteSmsLog(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return smsLogMapper.batchDeleteSmsLog(param);
    }

    public Page<SmsLog> querySmsLogPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = smsLogMapper.getSmsLogCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SmsLog> smsLogList = smsLogMapper.getSmsLogListByMap(param);
        page.setList(smsLogList);
        return page;
    }

    //发送短信
    @Override
    public void sendMessage(String phone, Integer codeType) throws Exception {

        //调用短信接口发送验证码
        String code = MathUtil.randomCode();
        SendSmsResponse sendSmsResponse = smSutil.sendSms(phone,"{code:"+code+"}");
        //保存短信的发送信息
        if (sendSmsResponse != null && sendSmsResponse.getBody().getCode().equals("OK")){
            //短信发送成功
            //保存验证码并且保存有效期，放在redis中
            String key = Constants.Redis.PREFIX_SMS+codeType+":"+phone;
            redisUtils.set(key,code,Constants.Duration.MINUTE_INT*50000);

            //增加短信表的记录
            SmsLog smsLog = new SmsLog();
            smsLog.setCodeType(codeType);
            smsLog.setPhone(phone);
            smsLog.setMessage(code);
            smsLog.setStatus(1);
            smsLog.setFailInfo("OK");
            smsLog.setCreatedUserId(0L);
            smsLog.setCreatdTime(new Date());
            smsLog.setUpdatedTime(new Date());
            smsLog.setIsDelete(0);
            smsLogMapper.insertSmsLog(smsLog);
        }

    }
}
